//引入express模块
const express = require("express");
//引入与数据库相关的模块
const pool = require("../pool.js");
//创建路由器对象(固定语法)
const router = express.Router();
//往该路由器中添加多个子路由器
// 用户登录

//用户注册接口
router.post("/register", (req, res, next) => {
  //console.log(md5('12345678'));
  // 获取用户名和密码信息手机号
  let uname = req.body.uname;
  let upassword = req.body.upassword;
  let uphone = req.body.uphone;
  console.log(uname);
  console.log(upassword);
  console.log(uphone);
  //以uname为条件进行查找操作，以保证用户名的唯一性
  let sql = "select count(uid) as count from sam_user where uname=?";
  pool.query(sql, [uname], (err, results) => {
    if (err) {
      next(err);
      return;
    }
    let count = results[0].count;
    if (count == 0) {
      // 将用户的相关信息插入到数据表
      sql = 'insert into sam_user values(null,?,?,?,"/mrtx/mrtx.png","1")';
      pool.query(sql, [uname, upassword, uphone], (err, results) => {
        if (err) {
          next(err);
          return;
        }
        res.send({ message: "ok", code: 200 });
      });
    } else {
      res.send({ message: "user exists", code: 201 });
    }
  });
});

// 用户登录接口
router.post("/login", (req, res, next) => {
  //获取用户名和密码信息
  let uphone = req.body.uphone;
  let upassword = req.body.upassword;
  let mphone=req.body.uphone;

  // SQL语句
  let sql = "SELECT * FROM sam_user WHERE uphone=? AND upassword=?";
  pool.query(sql, [uphone, upassword], (err, results) => {
    if (err) {
      next(err);
      return;
    }
    if (results.length == 0) {
      //登录失败
      res.send({ message: "login failed", code: 201 });
    } else {
      //登录成功
       // 查询用户是否为会员
      sql = "select count(mid) as count from sam_member where mphone=?";
      pool.query(sql,[mphone],(err,data)=>{
        if (err) {
          next(err);
          return;
        }
        let count=data[0].count
        if(count==0){
          res.send({ message: "ok", code: 200});    
        }else{
          res.send({ message: "ok", code: 200,data:mphone});   
        }
      })
     
    }
  });
});
// 用户查询
router.post("/loginok", (req, res, next) => {
  //获取手机号
  let uphone = req.body.uphone;
  // SQL语句
  let sql = "SELECT * FROM sam_user WHERE uphone=?";
  pool.query(sql, [uphone], (err, results) => {
    if (err) {
      next(err);
    } 
    res.send({ message: "ok", code: 200,data:results });
  });
});

// 会员注册接口
router.post("/vipreg", (req, res, next) => {
  //获取用户会员名和会员手机号信息
  let mname = req.body.mname;
  let mphone = req.body.mphone;
  console.log(mname);
  console.log(mphone);
  //以mphone为条件进行查找操作，以保证会员的唯一性
  let sql = "select count(mid) as count from sam_member where mphone=?";
  pool.query(sql, [mphone], (err, results) => {
    if (err) {
      next(err);
      return;
    }
    let count = results[0].count;
    if (count == 0) {
      // 将用户的相关信息插入到数据表
      let mtime = new Date();
      console.log(mtime);
      sql = "insert into sam_member values(null,?,?,?)";
      pool.query(sql, [mphone, mname, mtime], (err, results) => {
        if (err) {
          res.send({message:'错误',code:202})
          return;
        }
        res.send({ message: "ok", code: 200 });
      });
    } else {
      res.send({ message: "member exists", code: 201 });
    }
  });
});
//会员绑定接口
router.post("/viplog", (req, res, next) => {
  //获取用户会员名和会员手机号信息
  let mphone = req.body.mphone;
  let mname = req.body.mname;
  // SQL语句
  let sql =
    "SELECT mid,mname,mphone FROM sam_member WHERE mphone=? AND mname=?";
  pool.query(sql, [mphone, mname], (err, results) => {
    if (err) {
      next(err);
    }
    if (results.length == 0) {
      //绑定失败
      res.send({ message: "绑定失败", code: 201 });
    } else {
      //绑定成功
      res.send({ message: "ok", code: 200, result: results });
    }
  });
});
// 会员查询
router.post("/loginvip", (req, res, next) => {
  //获取手机号
  let mphone = req.body.mphone;
  // SQL语句
  let sql = "SELECT * FROM sam_member WHERE mphone=?";
  pool.query(sql, [mphone], (err, results) => {
    if (err) {
      next(err);
    } 
    res.send({ message: "ok", code: 200,data:results });
  });
});

// 用户地址保存
router.post("/readds", (req, res, next) => {
  //获取用户id和用户地址信息
  let user_id = req.body.uid;
  let receiver = req.body.name;
  let cellphone = req.body.phone;
  let regres = req.body.regres;
  let addres = req.body.addres;
  let sql =
    "select count(aid) as count from sam_receiver_address where user_id=?";
  pool.query(sql, [user_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    let count = data[0].count;
    let is_default = false;
    if (count == 0) {
      is_default = true;
    }
    sql = "insert into sam_receiver_address values(null,?,?,?,?,?,?)";
    pool.query(
      sql,
      [user_id, receiver, cellphone, regres, addres, is_default],
      (err, data) => {
        if (err) {
          next(err);
          return;
        }
        res.send({ message: "ok", code: 200 });
      }
    );
  });
});
// 用户地址接口
router.get("/address", (req, res, next) => {
  //获取用户id和用户地址信息
  let user_id =req.query.uid;
  console.log(user_id);
  let sql = "select * from sam_receiver_address where user_id=?;"
  pool.query(sql, [user_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ message: "ok", code: 200, data: data });
  });
});

module.exports = router;
